package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.entity.*;
import com.his.mapper.NonDrugItemRecordMapper;
import com.his.service.INonDrugItemRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 检查项检验项处置项记录表(开立的) 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class NonDrugItemRecordServiceImpl extends ServiceImpl<NonDrugItemRecordMapper, NonDrugItemRecord> implements INonDrugItemRecordService {

    @Resource
    private NonDrugItemRecordMapper mapper;

    @Resource
    private NonDrugItemRecordMapper nonDrugItemRecordMapper;

    //amount字段不知如何处理
    @Override
    public ResponseResult myInsert(NonDrugItemRecord nonDrugItemRecord) {
        Long count = 1L;
        Integer code = 0;
        String message = "";

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long staffId = loginUser.getUser().getId();


        try {

            count = mapper.getMaxId();
            nonDrugItemRecord.setId(count+1);
            nonDrugItemRecord.setStatus(1);
            nonDrugItemRecord.setCreatetime(LocalDateTime.now());
            nonDrugItemRecord.setCreatestaffid(staffId);
            if (0 == mapper.insert(nonDrugItemRecord)) {
                throw new Exception();
            }

            message = "插入成功";
            code = 201;
        }
        catch (Exception err) {
            message = "插入失败";
            code = 400;
        }

        return new ResponseResult(code, message, nonDrugItemRecord);
    }

    @Override
    public ResponseResult myDelete(NonDrugItemRecord nonDrugItemRecord) {
        Integer code = 0;
        String message = "";

        try {
            if (0 == mapper.deleteById(nonDrugItemRecord)) {
                throw new Exception();
            }
            message = "删除成功";
            code = 201;



        }
        catch (Exception err) {
            message = "删除失败";
            code = 400;
        }

        return new ResponseResult(code, message, null);
    }

    @Override
    public ResponseResult myUpdate(NonDrugItemRecord nonDrugItemRecord) {
        Integer code = 0;
        String message = "";

        try {

            if (0 == mapper.updateById(nonDrugItemRecord)) {
                throw new Exception();
            }

            message = "修改成功";
            code = 201;
        }
        catch (Exception err) {
            message = "修改失败";
            code = 400;
        }

        return new ResponseResult(code, message, nonDrugItemRecord);
    }

    // 尚峰

    @Override
    public ResponseResult selectPatientById(Registration registration) {
        List<Registration> registrations=nonDrugItemRecordMapper.selectPatientById(registration.getDeptid());
        if (Objects.isNull(registrations)) {
            return new ResponseResult(250, "查找信息失败", null);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("registration",registrations);
        return new ResponseResult(200, "查找信息成功", map);
    }

    @Override
    public ResponseResult selectAllRecord() {
        List<NonDrugItemRecord>nonDrugItemRecords=nonDrugItemRecordMapper.selectAllRecord();
        if (Objects.isNull(nonDrugItemRecords)) {
            return new ResponseResult(250, "查找信息失败", null);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("nonDrugItemRecord",nonDrugItemRecords);
        return new ResponseResult(200, "查找信息成功", map);
    }

    @Override
    public ResponseResult updateRecord(NonDrugItemRecord nonDrugItemRecord) {
        NonDrugItemRecord nonDrugItemRecord1=nonDrugItemRecordMapper.selectRecordById(nonDrugItemRecord.getRegistrationid());
        if (Objects.isNull(nonDrugItemRecord1)) {
            return new ResponseResult(250, "查找信息失败", null);
        }
        nonDrugItemRecord1.setCheckparts(nonDrugItemRecord.getResultimgurllist());
        nonDrugItemRecord1.setCheckresult(nonDrugItemRecord.getCheckresult());
        nonDrugItemRecordMapper.updateRecord(nonDrugItemRecord1);
        return new ResponseResult(200, "更新信息成功", null);
    }

    @Override
    public ResponseResult insertRecord(NonDrugItemRecord nonDrugItemRecord) {
        LocalDateTime localDateTime=LocalDateTime.now();
        nonDrugItemRecord.setCreatetime(localDateTime);
        nonDrugItemRecord.setLogdatetime(localDateTime);
        nonDrugItemRecord.setExcutetime(localDateTime);
        nonDrugItemRecordMapper.insertRecord(nonDrugItemRecord);
        return new ResponseResult(200, "添加信息成功", null);
    }
}
